package com.example.design.nioandnettty.practice.任务队列;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

/**
 * @Description TODO
 * @date 2023/9/22 8:55
 * @Author liuzhihui
 * @Version 1.0
 */
public class NettyServerTaskQueueHandler extends ChannelInboundHandlerAdapter {
    /**
     * 读取事件
     * @param ctx
     * @param msg
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {

        // 添加任务队列一
        ctx.channel().eventLoop().execute(()->{
            try {
                Thread.sleep(10000);
                ctx.writeAndFlush(Unpooled.copiedBuffer("hello,客户端000~", CharsetUtil.UTF_8));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        // 添加任务队列二
        ctx.channel().eventLoop().execute(()->{
            try {
                Thread.sleep(10000);
                ctx.writeAndFlush(Unpooled.copiedBuffer("hi,客户端000~", CharsetUtil.UTF_8));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }
}
